﻿using DataLibrary;
using System;
using System.Linq;
using System.Windows.Forms;

namespace TicketSystem
{
    public partial class Form_RoleRightUpdate : Form
    {
        public Form_RoleRightUpdate()
        {
            InitializeComponent();
        }
        private TicketSystemEntities tse = new TicketSystemEntities();
        public int RoleId;
        public string RoleName;
        private void btn_Save_Click(object sender, EventArgs e)
        {
            var role = tse.Roles.Where(u => u.RoleID == RoleId).FirstOrDefault();
            role.RoleName = this.txt_Role.Text;
            tse.Configuration.ValidateOnSaveEnabled = false;
            if (tse.SaveChanges() >= 0)//可能用户只想改变角色权限 不改变角色名称 所以这么判断受影响的行数为0
            {
                var phone = tse.RoleRights.Where(u => u.RoieID == RoleId && u.Rights.RightName == "显示电话").Count();
                var Print = tse.RoleRights.Where(u => u.RoieID == RoleId && u.Rights.RightName == "可以打印数据").Count();
                var Export = tse.RoleRights.Where(u => u.RoieID == RoleId && u.Rights.RightName == "可以导出数据").Count();
                var Update = tse.RoleRights.Where(u => u.RoieID == RoleId && u.Rights.RightName == "最高权限").Count();

                if (this.checkUpdate.Checked)
                {
                    if (Update == 0)
                    {
                        RoleRights roleright = new RoleRights();
                        roleright.RoieID = RoleId;
                        roleright.RightID = tse.Rights.Where(u => u.RightName == "最高权限").FirstOrDefault().RightID;
                        tse.RoleRights.Add(roleright);
                        if(phone==0)
                        {
                            RoleRights a = new RoleRights();
                            a.RoieID = RoleId;
                            a.RightID = tse.Rights.Where(u => u.RightName == "显示电话").FirstOrDefault().RightID;
                            tse.RoleRights.Add(a);
                        }
                        if (Print == 0)
                        {
                            RoleRights b = new RoleRights();
                            b.RoieID = RoleId;
                            b.RightID = tse.Rights.Where(u => u.RightName == "可以打印数据").FirstOrDefault().RightID;
                            tse.RoleRights.Add(b);
                        }
                        if (Export == 0)
                        {
                            RoleRights c = new RoleRights();
                            c.RoieID = RoleId;
                            c.RightID = tse.Rights.Where(u => u.RightName == "可以导出数据").FirstOrDefault().RightID;
                            tse.RoleRights.Add(c);
                        }
                    }
                }
                else
                {
                    if (Update >= 1)
                    {
                        var roleright = tse.RoleRights.Where(u => u.RoieID == RoleId && u.Rights.RightName == "最高权限").FirstOrDefault();
                        tse.RoleRights.Remove(roleright);
                    }
                    if (this.checkPhone.Checked)
                    {
                        if (phone == 0)
                        {
                            RoleRights a = new RoleRights();
                            a.RoieID = RoleId;
                            a.RightID = tse.Rights.Where(u => u.RightName == "显示电话").FirstOrDefault().RightID;
                            tse.RoleRights.Add(a);
                        }
                    }
                    else
                    {
                        if (phone >= 1)
                        {
                            var a = tse.RoleRights.Where(u => u.RoieID == RoleId && u.Rights.RightName == "显示电话").FirstOrDefault();
                            tse.RoleRights.Remove(a);
                        }
                    }
                    if (this.checkPrint.Checked)
                    {
                        if (Print == 0)
                        {
                            RoleRights b = new RoleRights();
                            b.RoieID = RoleId;
                            b.RightID = tse.Rights.Where(u => u.RightName == "可以打印数据").FirstOrDefault().RightID;
                            tse.RoleRights.Add(b);
                        }
                    }
                    else
                    {
                        if (Print >= 1)
                        {
                            var b = tse.RoleRights.Where(u => u.RoieID == RoleId && u.Rights.RightName == "可以打印数据").FirstOrDefault();
                            tse.RoleRights.Remove(b);
                        }
                    }
                    if (this.checkExport.Checked)
                    {
                        if (Export == 0)
                        {
                            RoleRights c = new RoleRights();
                            c.RoieID = RoleId;
                            c.RightID = tse.Rights.Where(u => u.RightName == "可以导出数据").FirstOrDefault().RightID;
                            tse.RoleRights.Add(c);
                        }
                    }
                    else
                    {
                        if (Export >= 1)
                        {
                            var c = tse.RoleRights.Where(u => u.RoieID == RoleId && u.Rights.RightName == "可以导出数据").FirstOrDefault();
                            tse.RoleRights.Remove(c);
                        }
                    }

                }
               
                tse.SaveChanges();
                this.Close();
            }
        }

        private void Form_RoleRightUpdate_Load(object sender, EventArgs e)
        {
            var roleright = tse.RoleRights.Where(u => u.RoieID == RoleId).Select(u => new { u.Rights.RightName }).ToList();
            for (var j = 0; j < roleright.Count; j++)
            {  
                if (roleright[j].RightName == "最高权限")
                {
                    this.checkPhone.Checked = true;
                    this.checkPrint.Checked = true;
                    this.checkExport.Checked = true;
                    this.checkUpdate.Checked = true;
                }
                else
                {
                    if (roleright[j].RightName == "显示电话")
                    {
                        this.checkPhone.Checked = true;
                    }
                    if (roleright[j].RightName == "可以打印数据")
                    {
                        this.checkPrint.Checked = true;
                    }
                    if (roleright[j].RightName == "可以导出数据")
                    {
                        this.checkExport.Checked = true;
                    }
                }
            }
            this.txt_Role.Text = RoleName;
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void checkUpdate_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkUpdate.Checked == true)
            {
                this.checkPhone.Checked = true;
                this.checkPhone.Enabled = false;
                this.checkPrint.Checked = true;
                this.checkPrint.Enabled = false;
                this.checkExport.Checked = true;
                this.checkExport.Enabled = false;
            }
            if (this.checkUpdate.Checked == false)
            {
                this.checkPhone.Enabled = true;
                this.checkPrint.Enabled = true;
                this.checkExport.Enabled = true;
                this.checkPhone.Checked = false;
                this.checkPrint.Checked = false;
                this.checkExport.Checked = false;
            }
        }


    }
}
